<?php
namespace app\extensions\lib\push;

use support\Redis;

class BasePush
{
    const EVENT_DEVICE_PUSH_LOG = 'sdk_device_push_message_log';
    const EVENT_SKD_REQUEST = 'sdk_push_request_log';


    public static function shushDefaultData($name,$properties,$is_add=true){
        $lists = [
            self::EVENT_DEVICE_PUSH_LOG => [
                'request_id',
                '#ip',
                '#time',
                'os_id',
                'cate_id',
                'game_id',
                'package_id',
                'token',
                'code',
                'status',
                'sdk_info',
                'result',
                'click_status',
                'click_time',
                'send_status',
                'send_time',
                'task_id',
                'title',
                'content',
                'image',
                'message_type',
                'request_time',
            ],
            self::EVENT_SKD_REQUEST => [
                '#ip',
                '#time',
                'request_id',
                'cate_id',
                'game_id',
                'package_id',
                'plat_count',
                'finish_time',
            ]
        ];
        $defaultValues = [
            self::EVENT_DEVICE_PUSH_LOG => [
                'send_status' => '0',
                'click_status' => '0',
            ],
            self::EVENT_SKD_REQUEST => [
                'plat_count' => '0'
            ],
        ];
        $columns = $lists[$name] ?? [];
        $new_properties = [];
        foreach ($columns as $column){
            if($is_add || (!$is_add && isset($properties[$column]))){
                $defaultValue = $defaultValues[$name][$column] ?? '';
                $newValue = $properties[$column] ?? $defaultValue;
                !is_array($newValue) && $newValue = strval($newValue);
                $new_properties[$column] = $newValue;
            }

        }
        //默认字段值
        if($is_add){
            $new_properties['#time'] = date('Y-m-d H:i:s');
        }

        return $new_properties;

    }



    public static function defaultReturn($success = 0, $error = 0)
    {
        return ['success' => $success, 'error' => $error];
    }

    public static function blackSkip(&$tokenInfo,$request_id){
        $udids = $tokenInfo['udids'] ?? '';
        $tokens = $tokenInfo['tokens'];
        $tokensArray = explode(',',$tokens);
        $udidsArray = explode(',',$udids);
        $blacklist = Redis::lrange(Push::getBlackList($request_id),0,-1);//获取当前请求的黑名单
        if(empty($blacklist)) return;
        $array_diff = array_values(array_intersect($udidsArray,$blacklist));
        foreach ($array_diff as $udid){
            $index = array_search($udid,$udidsArray);
            if($index!==false){
                unset($udidsArray[$index]);
                unset($tokensArray[$index]);
            }
        }

        $tokenInfo['udids'] = implode(',',$udidsArray);
        $tokenInfo['tokens'] = implode(',',$tokensArray);

    }

    public static function  requestRecall(&$tokenInfo,$request_id){

        if(self::isRequestRecall($request_id)){
            $tokenInfo['udids'] = '';
            $tokenInfo['tokens'] = '';
        }
    }

    public static function isRequestRecall($request_id){
        $datasKey = \app\extensions\lib\push\Push::getRequestData($request_id);
        $recall = Redis::hGet($datasKey,'recall');
        return $recall;
    }



}